package org.enlongWu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.enlongWu.mapper.ApplicationMapper;
import org.enlongWu.pojo.*;
import org.enlongWu.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationMapper mapper;

    @Override
    public List<ApplicationState> list() {
        return mapper.list();
    }

    @Override
    public Application findById(Integer applicationId) {
        return mapper.searchById(applicationId);
    }

    @Override
    public Result handleApplication(Integer applicationId,String toState) {

        Application ap = mapper.searchById(applicationId);
        if (ap.getState().equals(toState)){
            return Result.error("本来就是"+toState);
        }else {
            int i = mapper.updateState(applicationId,toState);
            if (i==1){
                return Result.success("处理成功");
            }else {
                return Result.error("失败 by applicationServiceImpl");
            }
        }

    }

    @Override
    public Integer addApplication(Application application) {
        Integer id = mapper.insertApplication(application);
        return id;
    }

    @Override
    public PageBean<Application> listFindById(Integer pageNum, Integer pageSize, List<Integer> ids) {
        //1.创建PageBean对象
        PageBean<Application> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<Application> as = mapper.applicationListByIdList(ids);//查询到的申请实体列表
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Application> p = (Page<Application>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

}
